package com.zx.cloud.service;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zx.cloud.domain.Dict;
import com.zx.cloud.persistence.ClientInfoMapper;
import com.zx.cloud.persistence.DictMapper;

@Service
public class FaceContrastService {
	
	 private static final Logger LOGGER = LoggerFactory.getLogger(FaceContrastService.class);
	@Autowired
    private ClientInfoMapper clientInfoMapper;
	
	@Autowired
	private DictMapper dictMapper;
//	 public static final String api_id = "cdf14abe2dae4922b9704f5bc5115625";
//	 public static final String api_secret = "54b232231f054d8eaecb293c9488fc01";
//	 public static final String  historical_selfie_url="http://47.100.111.13:8081/media/01/100170/a4521e25b997856d826b06b8475baedf.jpg";
//	 public static final String  selfie_url="http://47.100.111.13:8081/media/01/100168/2229cf51684d658c1215871f3c3d0bbb.jpg";
//	 public static final String POST_URL = "https://cloudapi.linkface.cn/identity/historical_selfie_verification";
	public  boolean   HttpClientPost(String  historical_selfie_url,String  selfie_url) throws IOException {
		boolean   flag=true;
		Dict dict = new Dict();
		dict.setParentCode("FC0001");
		List<Dict> dicts = dictMapper.getDitByParentCode(dict);
		String api_id =null;
		String api_secret =null;
		String post_url =null;
		
		String flagStr =null;
		if(null != dicts && dicts.size() > 0){
			for(Dict tmp : dicts){
				if("api_id".equals(tmp.getName())){
					api_id = tmp.getValue();
				}
				if("api_secret".equals(tmp.getName())){
					api_secret = tmp.getValue();
				}
				if("post_url".equals(tmp.getName())){
					post_url = tmp.getValue();
				}
				
				if("mark".equals(tmp.getName())){
					flagStr = tmp.getValue();
				}
			}
		}
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost post = new HttpPost(post_url);
		StringBody id = new StringBody(api_id);
		StringBody secret = new StringBody(api_secret);
		StringBody fileBody1 = new StringBody(historical_selfie_url);
		StringBody fileBody2 = new StringBody(selfie_url);
		
		MultipartEntity entity = new MultipartEntity();
		entity.addPart("historical_selfie_url", fileBody1);
		entity.addPart("selfie_url", fileBody2);
		entity.addPart("api_id", id);
		entity.addPart("api_secret", secret);
		post.setEntity(entity);

		HttpResponse response = null;
		try {
			response = httpclient.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entitys = response.getEntity();
				BufferedReader reader = new BufferedReader(new InputStreamReader(entitys.getContent()));
				String line = reader.readLine();
				LOGGER.info("**********************人脸比对结果："+line+"**************************");
				JSONObject dataJson = new JSONObject(line);
				double confidence= (double) dataJson.get("confidence");
				LOGGER.info("**********************人脸比对分数："+confidence+"**************************");
				double  flagConfidence=Double.parseDouble(flagStr);//
				if(confidence <flagConfidence ){
					flag=false;
				}
			} else {
				HttpEntity r_entity = response.getEntity();
				String responseString = EntityUtils.toString(r_entity);
				LOGGER.info("**********************人脸比对结果错误码是："+ response.getStatusLine().getStatusCode() + "  "+ response.getStatusLine().getReasonPhrase()+"**************************");
				LOGGER.info("**********************人脸比对结果出错原因是：" + responseString+"**************************");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			httpclient.getConnectionManager().shutdown();
		}

		 return  flag;
	}

}